package jp.eisbahn.eclipse.plugins.twitterclipse.internal;

import jp.eisbahn.eclipse.plugins.twitterclipse.HttpRequestTimeoutException;
import jp.eisbahn.eclipse.plugins.twitterclipse.TwitterCommunicationException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.IPatternMatchListenerDelegate;
import org.eclipse.ui.console.PatternMatchEvent;
import org.eclipse.ui.console.TextConsole;

public class ConsoleObserver implements IPatternMatchListenerDelegate {
	
	private boolean matched;

	public void connect(TextConsole console) {
		matched = false;
	}

	public void disconnect() {
		if (matched) {
			try {
				TwitterclipsePlugin plugin = TwitterclipsePlugin.getDefault();
				TwitterclipseConfig config = plugin.getTwitterclipseConfiguration();
				if (config.isUpdateStatusAtNullPointerException()) {
					plugin.updateStatus(config.getStatusAtNullPointerException());
				}
			} catch(TwitterCommunicationException e) {
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				MessageDialog.openError(shell, "Twitterclipse", e.getMessage());
			} catch(HttpRequestTimeoutException e) {
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				MessageDialog.openError(shell, "Twitterclipse", e.getMessage());
			}
		}
		matched = false;
	}

	public void matchFound(PatternMatchEvent event) {
		matched = true;
	}

}
